
*=============================================
* Table C.2 IV estimates of the effect of road toll on car ownership based on 2008 location

// IV estimates
// IV: road toll rates from 2015-2017 combined with 2008 work commute
*=============================================

use    "${dataout}MainDataset" , clear

***********************************
***** Choose years to keep!  ******
keep if year==2008 | year>=2015
***********************************

keep if couple==1

/****************************************************************************/
/*******  Adjust road toll in 2015-2017 to reflect 2008 grunnkrets **********/
/****************************************************************************/
 
/*=== STEP 1: Get 2008-grunnkrets data =======*/
preserve
keep if year==2008
keep familienr indid1 indid2 grkrets grk_bed1 grk_bed2
rename grkrets grkrets_2008 
rename grk_bed1 grk_bed1_2008
rename grk_bed2 grk_bed2_2008
sort familienr indid1 indid2
tempfile helpfile
save `helpfile'
restore

sort familienr indid1 indid2
merge m:1 familienr indid1 indid2 using  `helpfile' , keep(match master)
drop _merge

/*=== STEP 2: Destring grunnkrets 2008 =======*/
destring grkrets_2008, gen(grkrets_2008_num)
destring grk_bed1_2008, gen(grk_bed1_2008_num)
destring grk_bed2_2008, gen(grk_bed2_2008_num)

/* === STEP 3: Adding info on 2015-2017 road toll to 2008 grk-grk-pairs ===== */ 
forvalues y=15(1)17 {  
	preserve
		use "${datain}distances", clear
		des 
		rename takst_r20`y' toll_`y'
		keep grk1 grk2 toll_`y'
		tempfile grk1grk2
		save `grk1grk2'
	restore

	// For individual 1 and 2 in household: 
	forvalues i=1(1)2 {		
		capt drop grk1
		gen grk1=grkrets_2008_num /*Use grunnkrets HOME from 2008*/

		/* to work  */
		capt drop grk2
		gen grk2=grk_bed`i'_2008_num   /*Use grunnkrets WORK from 2008*/
		merge m:1 grk1 grk2  using `grk1grk2', keep(match master)
		drop _merge
		rename toll_`y' TO_toll_`y'_`i'

		/* changing the order of grks */
		rename grk2 temp
		rename grk1 grk2
		rename temp grk1

		/* from work */
		merge m:1 grk1 grk2  using `grk1grk2', keep(match master)
		drop _merge
		rename toll_`y' FROM_toll_`y'_`i'

		drop grk1 grk2

		/* Creating averages of to/from work, where available */
		foreach var in toll_`y'_`i' {
			gen `var' = (TO_`var' + FROM_`var') / 2
			replace `var' = TO_`var' if `var' == .
			replace `var' = FROM_`var' if `var' == .
			drop TO_`var' FROM_`var'
		}
	}
	/* === STEP 4: Creating averages for both individuals ======== */
	egen toll_`y'_fam_mean=rowmean(toll_`y'_1 toll_`y'_2) 
}

/* === STEP 5: Make adjusted road toll variable  ======== */

// First: inflation adjust road toll variables
sum KPI_2017

local varlist toll_15_fam_mean toll_16_fam_mean toll_17_fam_mean  
foreach v in `varlist' {
gen `v'_KPI = `v'/KPI_2017 * 100
}

// Create instrument Z
capt gen toll_fam_mean_adj_KPI=.
replace toll_fam_mean_adj_KPI=toll_fam_mean        if year==2008 /*2008: no difference between X and Z*/
replace toll_fam_mean_adj_KPI=toll_15_fam_mean_KPI if year==2015 
replace toll_fam_mean_adj_KPI=toll_16_fam_mean_KPI if year==2016  
replace toll_fam_mean_adj_KPI=toll_17_fam_mean_KPI if year==2017  

// Reduced form and IV estimation ==============================================
keep if year>=2015  /*Only look at 2015-2017*/

* Select instrument and treatvar to instrument for
local treatvar1 toll_fam_mean
local IV1 toll_fam_mean_adj_KPI

** Select variables to loop over: 
local outcomelist BEV_fam_yes ICE_fam_count             
local treatvar2list ptl_fam_km_mean     

** Regressions - loop over: outcome, treatment, year
foreach outcome in `outcomelist'{
	foreach treatvar2 in `treatvar2list'{
		// (a): IV estimation ==================================================	
		mata: mata mlib index   /*Need to include this to make ivreghdfe work*/

		ivreghdfe `outcome'  `treatvar2'  $age    $distance  $time  $publictime  $publicquality  (`treatvar1' = `IV1') , ///
		absorb($FE   $household  $income  $employment  $education  i.year ) cluster($clustervar) ///
		first ffirst savefirst savefprefix(first_) rf saverf saverfprefix(reduced_) 
		/**/
		summarize `outcome' if e(sample)==1
		estadd scalar MeanDep = r(mean), replace
		local MeanDep = r(mean)  
		summarize `treatvar1' if e(sample)==1
		estadd scalar Treat1 = r(mean), replace
		local Treat1 = r(mean)  
		summarize `IV1' if e(sample)==1
		estadd scalar Treat1IV = r(mean), replace
		local Treat1IV = r(mean) 
		summarize `treatvar2' if e(sample)==1
		estadd scalar Treat2 = r(mean), replace
		local Treat2 = r(mean) 
		/**/
		summarize `outcome' if e(sample)==1 & `treatvar1'==0 & `treatvar2'==0   
		estadd scalar MeanDepCon = r(mean), replace  
		local MeanDepCon = r(mean) 
		/**/
		local Proportional1 = _b[`treatvar1']/`MeanDepCon'   
		estadd scalar Proportional1 =  `Proportional1' 
		local Elasticity1 = (_b[`treatvar1']/`MeanDep') / (1/`Treat1')  
		estadd scalar Elasticity1 =  `Elasticity1'
		/**/
		local Proportional2 = _b[`treatvar2']/`MeanDepCon'  
		estadd scalar Proportional2 =  `Proportional2' 
		local Elasticity2 = (_b[`treatvar2']/`MeanDep') / (1/`Treat2')   
		estadd scalar Elasticity2 =  `Elasticity2' 
		/**/
		estadd local year="2015-2017" , replace
		estadd local grkFE="\checkmark", replace
		estadd local grkbFE="\checkmark", replace
		estadd local HHcon="\checkmark", replace
		estadd local HHWork="\checkmark", replace
		estadd local HHFE="", replace
		estadd local PublicTime="\checkmark", replace
		eststo tableC2reg4_`outcome'
		* Store estimates
		estimates save "${ster}TableC2_`outcome'_SecondStage" , replace

		** Mark main sample
		capt drop MainSample
		gen MainSample=0
		replace MainSample=1 if e(sample)==1

		// (b) First stage =====================================================
		estimates restore first_`treatvar1'

		estadd local year="2015-2017" , replace
		estadd local grkFE="\checkmark", replace
		estadd local grkbFE="\checkmark", replace
		estadd local HHcon="\checkmark", replace
		estadd local HHWork="\checkmark", replace
		estadd local HHFE="", replace
		estadd local PublicTime="\checkmark", replace
		* Store estimates
		estimates save "${ster}TableC2_`outcome'_FirstStage" , replace

		// (c) Reduced form ====================================================
		estimates restore reduced_`outcome'

		summarize `outcome' if e(sample)==1
		estadd scalar MeanDep = r(mean), replace
		local MeanDep = r(mean) 
		summarize `treatvar1' if e(sample)==1
		estadd scalar Treat1 = r(mean), replace
		local Treat1 = r(mean) 
		summarize `IV1' if e(sample)==1
		estadd scalar Treat1IV = r(mean), replace
		local Treat1IV = r(mean) 
		summarize `treatvar2' if e(sample)==1
		estadd scalar Treat2 = r(mean), replace
		local Treat2 = r(mean)  
		/**/
		summarize `outcome' if e(sample)==1 & `IV1'==0 & `treatvar2'==0  
		estadd scalar MeanDepCon = r(mean), replace   
		local MeanDepCon = r(mean) 
		/**/
		local Proportional1 = _b[`IV1']/`MeanDepCon'   
		estadd scalar Proportional1 =  `Proportional1' 
		local Elasticity1 = (_b[`IV1']/`MeanDep') / (1/`Treat1IV') 
		estadd scalar Elasticity1 =  `Elasticity1'
		/**/
		local Proportional2 = _b[`treatvar2']/`MeanDepCon'  
		estadd scalar Proportional2 =  `Proportional2'
		local Elasticity2 = (_b[`treatvar2']/`MeanDep') / (1/`Treat2')  
		estadd scalar Elasticity2 =  `Elasticity2'
		/**/
		estadd local year="2015-2017" , replace
		estadd local grkFE="\checkmark", replace
		estadd local grkbFE="\checkmark", replace
		estadd local HHcon="\checkmark", replace
		estadd local HHWork="\checkmark", replace
		estadd local HHFE="", replace
		estadd local PublicTime="\checkmark", replace

		eststo reduced_`outcome'
		estimates save "${ster}TableC2_`outcome'_ReducedForm" , replace

		// (d): "Normal"  OLS estimation with toll_fam_mean ====================
		reghdfe `outcome' `treatvar1'  `treatvar2'  $age   $distance  $time  $publictime  $publicquality  if MainSample==1 , ///
		absorb($FE   $household  $income  $employment  $education  i.year   ) vce(cluster $clustervar) 
		/**/
		summarize `outcome' if e(sample)==1
		estadd scalar MeanDep = r(mean), replace
		local MeanDep = r(mean) 
		summarize `treatvar1' if e(sample)==1
		estadd scalar Treat1 = r(mean), replace
		local Treat1 = r(mean)  
		summarize `IV1' if e(sample)==1
		estadd scalar Treat1IV = r(mean), replace
		local Treat1IV = r(mean) 
		summarize `treatvar2' if e(sample)==1
		estadd scalar Treat2 = r(mean), replace
		local Treat2 = r(mean) 
		/**/
		summarize `outcome' if e(sample)==1 & `treatvar1'==0 & `treatvar2'==0   
		estadd scalar MeanDepCon = r(mean), replace  
		local MeanDepCon = r(mean) 
		/**/
		local Proportional1 = _b[`treatvar1']/`MeanDepCon'  
		estadd scalar Proportional1 =  `Proportional1' 
		local Elasticity1 = (_b[`treatvar1']/`MeanDep') / (1/`Treat1')   
		estadd scalar Elasticity1 =  `Elasticity1'
		/**/
		local Proportional2 = _b[`treatvar2']/`MeanDepCon'   
		estadd scalar Proportional2 =  `Proportional2' 
		local Elasticity2 = (_b[`treatvar2']/`MeanDep') / (1/`Treat2')  
		estadd scalar Elasticity2 =  `Elasticity2'
		/**/

		estadd local year="2015-2017" , replace
		estadd local grkFE="\checkmark", replace
		estadd local grkbFE="\checkmark", replace
		estadd local HHcon="\checkmark", replace
		estadd local HHWork="\checkmark", replace
		estadd local HHFE="", replace
		estadd local PublicTime="\checkmark", replace
		eststo tableC2reg1_`outcome' 
		estimates save "${ster}TableC2_`outcome'_OLS" , replace
	}
}

* =============================================================================
// Making table from .ster files
* =============================================================================

local outcomelist   BEV_fam_yes ICE_fam_count
local treatvar1list toll_fam_mean_KPI     
local treatvar2list ptl_fam_km_mean    
local IV1list toll_fam_mean_adj_KPI     

foreach outcome in `outcomelist'{
	foreach treatvar1 in `treatvar1list'{
		foreach treatvar2 in `treatvar2list'{
			foreach IV1 in `IV1list'{
				
				eststo drop *
				estimates use "${ster}TableC2_`outcome'_OLS" 
				eststo
				estimates use "${ster}TableC2_`outcome'_SecondStage" 
				eststo
				estimates use "${ster}TableC2_`outcome'_FirstStage" 
				eststo
				estimates use "${ster}TableC2_`outcome'_ReducedForm" 
				estadd local MeanDep="" , replace
				estadd local Treat1="" , replace
				estadd local Treat1IV="" , replace
				estadd local Proportional1="" , replace
				estadd local Elasticity1="" , replace
				eststo

				// Latex  - Coeff only
				esttab est*  ///
				using  "${tables}TableC2fragment_`outcome'_coef.tex" ///
				, keep(`treatvar1'  `IV1') ///
				booktabs nonotes  coeflabels(`treatvar1'  "Road toll (NOK)"   `IV1' "Road toll 2008 commute (NOK)") ///
				b(5) se(5) star(* 0.10 ** 0.05 *** 0.01) fragment nomtitle nonumbers nodepvars nogap noobs nolines replace


				// Latex  - Controls only
				esttab est* ///
				using  "${tables}TableC2fragment_`outcome'_controls.tex" 
				, drop(*)   nonotes  nomtitle nonumbers   nodepvars  nogaps nolines  prehead("") posthead("") ///
				stats( N year grkFE grkbFE  HHWork HHcon  PublicTime  Treat1  Treat1IV  , ///
					label("N" "Year"  "Neighborhood residence FE" "Neighborhood work FE"  "Ind-work controls" "HH and ind controls" "Public transit (minutes)"  "Mean road toll (NOK)"  "Mean road toll 2008 commute (NOK)" ) fmt(%9.0fc a3) ) ///
				b(5) se(5) star(* 0.10 ** 0.05 *** 0.01) fragment   replace


				** Make table with Mean Outcome 
				esttab est* ///
				using  "${tables}TableC2fragment_`outcome'_meanOutcome.tex" 
				, drop(*)   nonotes  nomtitle nonumbers   nodepvars  nogaps nolines  prehead("") posthead("") ///
				  stats(MeanDep   , label("Mean outcome")  fmt(a3)) fragment replace
				  
				** Make table with F statistic (only for IV_SS)
				esttab est* ///
				using  "${tables}TableC2fragment_`outcome'_Fstat.tex" 
				, drop(*)   nonotes  nomtitle nonumbers   nodepvars  nogaps nolines  prehead("") posthead("") ///
				  stats(widstat  , label("F statistic (excl. instrument)" )  fmt(%9.0fc)) fragment replace
			}
		}
	}
}
